﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Manage
{
    public partial class MRight : MgConfig
    {
        protected string chkAll;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Verify("MRight.aspx");
                if (Request.QueryString["msg"] != null)
                {
                    if (Request.QueryString["msg"] != "Edit")
                    {
                        Message = MessageBox("权限添加成功！", MsgType.ok);
                    }
                    else
                    {
                        Message = MessageBox("权限修改成功！", MsgType.ok);
                    }
                }
                chkAll = GetGroup(string.Empty);
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            StringBuilder sbchk = new StringBuilder();
            foreach (string chk in hidchk.Value.Split('|'))
            {
                if (Request.Form[chk] != null)
                {
                    sbchk.Append(Request.Form[chk].ToString());
                    sbchk.Append(",");
                }
            }
            string strchk = string.Empty;
            if (sbchk != null && sbchk.ToString().Length > 0)
            {
                strchk = sbchk.ToString().Substring(0, sbchk.ToString().Length - 1);
            }
            try
            {
                if (btnSubmit.CommandName != "Edit")
                {
                    new BLL.Manage_Right().Add(new Jy123.Model.Manage_Right()
                    {
                        RName = txtRame.Value,
                        FMPage = strchk
                    });
                }
                else
                {
                    new BLL.Manage_Right().Update(new Jy123.Model.Manage_Right()
                    {
                        ID = int.Parse(hidrid.Value),
                        RName = txtRame.Value,
                        FMPage = strchk
                    });
                }
                Response.Redirect(string.Format("{0}?msg={1}", Request.Url.ToString(), btnSubmit.CommandName));
            }
            catch
            {
                Message = MessageBox("操作错误！", MsgType.error);
            }
        }
        private DataTable GetRight()
        {
            string sqlStr = "select (select pName from Manage_Page a where ID = b.upid) upName,id,upid,pName,url from Manage_Page b where upid<>0 group by upid,pname,url,id order by upid desc";
            return DBUtility.DbHelperSQL.Query(sqlStr).Tables[0];
        }
        private string GetGroup(string right)
        {
            string hBox = @"
                <h5 class=""chk""><label for=""chkBox{0}""><input id=""chkBox{0}"" type=""checkbox"" onclick=""selAll(this)"" />{1}</label></h5>
                <ul class=""chk"">{2}
                </ul>";
            string hItems = @"
                    <li><label for=""schk{0}""><input id=""schk{0}"" name=""schkBox{1}"" value=""{0}"" type=""checkbox"" {3} />{2}<span style=""color:red"">[{0}]</span></label></li>";
            StringBuilder strGroup = new StringBuilder();
            StringBuilder strPage = new StringBuilder();
            DataTable dt = GetRight();
            if (dt.Rows.Count > 0)
            {
                string pName = dt.Rows[0][0].ToString();
                string upid = dt.Rows[0][2].ToString();
                hidchk.Value = string.Format("schkBox{0}|", upid);
                if (right != string.Empty)
                {
                    right = string.Format(",{0},", right);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (pName != dt.Rows[i][0].ToString())
                    {
                        strGroup.AppendFormat(hBox, upid, pName, strPage.ToString());
                        pName = dt.Rows[i][0].ToString();
                        upid = dt.Rows[i][2].ToString();
                        strPage.Remove(0, strPage.Length);
                        hidchk.Value += string.Format("schkBox{0}|", upid);
                    }
                    string chked = string.Empty;
                    if (right.IndexOf(string.Format(",{0},", dt.Rows[i][1])) != -1)
                    {
                        chked = "checked=\"checked\"";
                    }
                    else
                    {
                        chked = string.Empty;
                    }
                    strPage.AppendFormat(hItems, dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3], chked);
                    if (i + 1 == dt.Rows.Count)
                    {
                        strGroup.AppendFormat(hBox, upid, pName, strPage.ToString());
                    }

                }
                hidchk.Value = hidchk.Value.Substring(0, hidchk.Value.Length - 1);
            }
            return strGroup.ToString();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Label rname = (Label)GridView1.Rows[e.NewEditIndex].FindControl("labRName");
            Label pitems = (Label)GridView1.Rows[e.NewEditIndex].FindControl("labPageItems");
            HiddenField hid = (HiddenField)GridView1.Rows[e.NewEditIndex].FindControl("hidID");
            txtRame.Value = rname.Text;
            btnSubmit.Text = "修 改";
            btnSubmit.CommandName = "Edit";
            hidrid.Value = hid.Value;
            chkAll = GetGroup(pitems.Text);
        }
    }
}
